package com.starbaba.stat.utils;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.message.BasicNameValuePair;

import com.starbaba.common.ResultJson;
import com.starbaba.common.db.CommBusiCommentVO;
import com.starbaba.core.utils.json.JsonUtils;
import com.starbaba.model.dictionary.IImgType;
import com.starbaba.model.utils.HttpConnectionUtils;

@SuppressWarnings("unused")
public class UpdateRedis implements Constant {

	// mredis正式环境地址
	public static String REDIS_URL = "http://xmiles.cn";
	// mredis测试环境地址
	// private final static String REDIS_URL = "http://112.74.79.60:14000/";

	private final static Map<String, String> operMap = new HashMap<String, String>();

	static {
		operMap.put(RPUSH, "/rpush");
		operMap.put(LLEN, "/llen");
		operMap.put(LSET, "/lset");
		operMap.put(SET, "/set");
	}

	public static void delKey(String key) {
		String url = REDIS_URL + "/mredis/del/del/?key=" + key;
		String result = HttpConnectionUtils.connection(url, HttpConnectionUtils.POST);
		System.out.println("删除缓存成功 Key : " + key + " URL:" + url + " result : " + result);
	}

	public static String get(String key) {
		String url = REDIS_URL + "/mredis/del/del/?key=" + key;
		String result = HttpConnectionUtils.connection(url, HttpConnectionUtils.POST);
		ResultJson resultJson = JsonUtils.fromJson(result, ResultJson.class);
		if (resultJson != null) {
			if (resultJson.status == 1) {
				return resultJson.msg;
			} else {
				return null;
			}
		}
		return null;
	}

	public static String hset(String map, String key, String value) {
		String url = REDIS_URL + "/mredis/oper/hset";
		ArrayList<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
		list.add(new BasicNameValuePair("map", map));
		list.add(new BasicNameValuePair("key", key));
		list.add(new BasicNameValuePair("value", value));
		String result = HttpConnectionUtils.connection(url, HttpConnectionUtils.POST, list);
		ResultJson resultJson = JsonUtils.fromJson(result, ResultJson.class);
		if (resultJson != null) {
			if (resultJson.status == 1) {
				return resultJson.msg;
			} else {
				return null;
			}
		}
		return null;
	}

	public static String operate(String operteType, String key, String... para) {
		String url = REDIS_URL + "/mredis/oper" + operMap.get(operteType);
		ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
		int index = 0;
		BasicNameValuePair pair;

		try {
			key = URLEncoder.encode(key, "utf-8");
			pair = new BasicNameValuePair("key", key);
			pairs.add(pair);
			for (String s : para) {
				s = URLEncoder.encode(s, "utf-8");
				pair = new BasicNameValuePair("para" + index, s);
				index++;
				pairs.add(pair);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String result = HttpConnectionUtils.connection(url, HttpConnectionUtils.POST, pairs);

		ResultJson resultJson = JsonUtils.fromJson(result, ResultJson.class);
		if (resultJson != null) {
			if (resultJson.status == 1) {
				return resultJson.msg;
			} else {
				return null;
			}
		}
		return null;

	}

	// 删除商家联系方式缓存
	public static void updateBusiPhone(String busiid, String busitype) {
		delKey("cbp:busiid_" + busiid + "busi_type_" + busitype); // 先删除
	}

	// 删除商家属性缓存
	public static void updateBusiAttr(String busiid, String busitype) {
		delKey("cba:id_" + busiid + "btype_" + busitype); // 先删除
	}

	// 删除jiayouzhan缓存
	public static void updateGasStation(String parkingid) {
		delKey("cgs:id_" + parkingid); // 先删除
		delKey("cgi:pid_" + parkingid);
	}

	// 删除商家信息缓存
	public static void updateBusiness(String parkingid) {
		delKey("cb:id_" + parkingid); // 先删除
	}

	public static void updateParking(String parkingid) {
		delKey("ccp:id_" + parkingid); // 先删除
	}

	public static void updatePromotion(String id) {
		// 删除缓存,车生活服务发现缓存不见了，再实时创建
		delKey("cbd:busiid_" + id);
	}

	public static void updateAllPhoto(String id, String objType) {
		delKey("cob:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_COMMENT);
		delKey("cob:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_MERCHANT);
		delKey("cob:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_OFFER);
		delKey("cob:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_PRICE);
		delKey("cob_thumb:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_COMMENT);
		delKey("cob_thumb:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_MERCHANT);
		delKey("cob_thumb:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_OFFER);
		delKey("cob_thumb:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_PRICE);
	}

	/**
	 * 删除商户价格图片
	 */
	public static void updatePricePhoto(String id, String objType) {
		delKey("cobp:objid_" + id + "obj_type_" + objType);
		delKey("cob_thumb:objid_" + id + "obj_type_" + objType + "img_type_" + IImgType.IMGTYPE_PRICE);

	}

	public static void updateComment(CommBusiCommentVO vo) {

		StringBuffer sf = new StringBuffer();
		sf.append(REDIS_URL);
		sf.append("/mredis/comment/updateComment?busiType=");
		sf.append(vo.getBusitype());
		sf.append("&busiid=");
		sf.append(vo.getBusiid());
		sf.append("&erviceid=");
		sf.append(vo.getServiceid());

		String url = sf.toString();

		String result = HttpConnectionUtils.connection(url, HttpConnectionUtils.POST);
	}

}
